<template>
  <div class="app-container">

    <el-card class="box-card"  style="border-radius: 10px;" v-show="showSearch" >
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
<!--      <el-form-item label="电路类型编号" prop="circuitTypeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.circuitTypeId"-->
<!--          placeholder="请输入电路类型编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="电路类型" prop="circuitTypeId">
        <el-select v-model="queryParams.circuitTypeId" placeholder="请选择类型" clearable size="small">
          <el-option v-for="(item, index) in circuitTypeList" :label="item.name" :value="item.id" :key="index"/>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="编码类型" prop="type">-->
<!--        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">-->
<!--          <el-option v-for="(item, index) in codeDataTypeEnum" :label="item.name" :value="item.value" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择类型" clearable size="small" style="min-width: 150px;">
          <el-option v-for="(item, index) in dataTypeEnum" :label="item.name" :value="item.value" :key="index"/>
        </el-select>
      </el-form-item>
      <el-form-item label="可用状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择类型" clearable size="small">
          <el-option v-for="(stateTypeItem, stateTypeIndex) in stateTypeEnum" :label="stateTypeItem.name" :value="stateTypeItem.value" :key="stateTypeIndex"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card class="box-card margin_t10"  style="border-radius: 10px;" >

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" width="80" align="center" prop="id" />
      <el-table-column label="电路类型" align="center" prop="circuitType.name"/>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="编码" align="center" prop="code" />

<!--      <el-table-column label="编码类型" align="center"  prop="type" >-->
<!--        <template v-if="scope.row.type" slot-scope="scope">-->
<!--          <span>{{GlobalVariable.getOneCodeType(scope.row.type)}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="数据类型" align="center"  prop="dataType" >
        <template v-if="scope.row.dataType" slot-scope="scope">
          <span>{{GlobalVariable.getOneDataType(scope.row.dataType)}}</span>
        </template>
      </el-table-column>

      <el-table-column label="可用状态" align="center" prop="state">
        <template v-if="scope.row.state" slot-scope="scope">
          <span>{{scope.row.state === "YES" ? '可用' : '禁用'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设置" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type === 'INPUT_PARAM'"
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            @click="openDataRules(scope.row)"
          >输入参数规则配置</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-card>

    <!-- 添加或修改电路属性参数模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电路类型" prop="circuitTypeId">
          <el-select v-model="form.circuitTypeId" placeholder="请选择类型" clearable size="small">
            <el-option v-for="(item, index) in circuitTypeList" :label="item.name" :value="item.id" :key="index"/>
          </el-select>
        </el-form-item>

        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>


<!--        <el-form-item label="编码类型" prop="type">-->
<!--          <el-select v-model="form.type" placeholder="请选择类型">-->
<!--            <el-option v-for="(item,index) of codeDataTypeEnum" :key="index" :value="item.value" :label="item.name">{{item.name}}</el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->

        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择类型">
            <el-option v-for="(item,index) of dataTypeEnum" :key="index" :value="item.value" :label="item.name">{{item.name}}</el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择类型" clearable size="small">
            <el-option v-for="(stateTypeItem, stateTypeIndex) in stateTypeEnum" :label="stateTypeItem.name" :value="stateTypeItem.value" :key="stateTypeIndex"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 规则脚本对话框 -->
    <el-dialog :close-on-click-modal="false" title="输入参数规则配置" :visible.sync="ruleScriptDialog" width="1200px" append-to-body>
      <rule-script-common :scene-name="sceneName" :scene-id="sceneId" :scene-type="1" @close="ruleScriptDialog = false" ref="refRuleScript" v-if="ruleScriptDialog"/>
    </el-dialog>
  </div>
</template>

<script>
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate } from "@/api/expert/codeDataTemplate";
import { allListCircuitType } from "@/api/expert/circuitType";
import ruleScriptCommon from "../common/ruleScriptCommon";

export default {
  name: "codeTemplate",
  components: {ruleScriptCommon},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电路属性参数模板表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        circuitTypeId: null,
        name: null,
        code: null,
        type: "INPUT_PARAM",
        dataType: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

// --------------------------------------------------------------------------

      // 电路类型表格数据
      circuitTypeList: [],
      // 状态枚举
      stateTypeEnum:this.GlobalVariable.stateTypeEnum,
      // 数据类型枚举
      dataTypeEnum:this.GlobalVariable.dataTypeEnum,
      // 编码枚举
      codeDataTypeEnum:this.GlobalVariable.codeDataTypeEnum,

      // 规则脚本弹窗
      ruleScriptDialog: false,

      // 场景id
      sceneId: undefined,
      // 场景名称
      sceneName: undefined,
      // 电路类型id
      circuitTypeId: undefined,
    };
  },
  created() {
    this.getList();
    this.getCircuitTypeList();
  },
  methods: {
    /** 查询电路属性参数模板列表 */
    getList() {
      this.loading = true;
      listTemplate(this.queryParams).then(response => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询电路类型列表 */
    getCircuitTypeList() {
      allListCircuitType().then(response => {
        this.circuitTypeList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        circuitTypeId: null,
        name: null,
        code: null,
        type: "INPUT_PARAM",
        dataType: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电路属性参数模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTemplate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电路属性参数模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.type = "INPUT_PARAM"
          if (this.form.id != null) {
            updateTemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除电路属性参数模板编号为"' + ids + '"的数据项？').then(function() {
        return delTemplate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edsta/template/export', {
        ...this.queryParams
      }, `edsta_template.xlsx`)
    },

//------------------------------------------------------------------------

    // 输入参数规则配置
    openDataRules(row){
      this.sceneId = row.id
      this.sceneName = row.name + " (" + row.code + ")"
      this.ruleScriptDialog = true;
    },
  }
};
</script>
